<script setup>
import "./style.css";
import { ref } from "vue";
import VueTree from "../VueD3Tree/index.vue";
import { fakeData1 } from "./fakeData";

const treeData = ref(fakeData1);

const nodeColor = {
  "食物": "blue",
  "水果": "red",
  "蔬菜": "green"
}

function handleNodeStyle({data}) {
  return {
            width: '60px',
            height: '30px',
            backgroundColor: nodeColor[data.name],
            color: '#fff',
            textAlign: 'center',
            lineHeight: '30px',
          }
}
</script>

<template>
  <div class="demo-main">
    <div>
      <h3>默认节点</h3>
      <div class="canvas-container">
        <VueTree :data="treeData" default-node-key="name" />
      </div>
    </div>
    <div>
      <h3>修改默认节点样式</h3>
      <div class="canvas-container">
        <VueTree
          :data="treeData"
          default-node-key="name"
          :default-node-style="{
            width: '60px',
            height: '30px',
            borderColor: 'red',
            backgroundColor: 'green',
            color: '#fff',
            textAlign: 'center',
            lineHeight: '30px',
          }"
        />
      </div>
    </div>
    <div>
      <h3>通过函数修改默认节点样式</h3>
      <div class="canvas-container">
        <VueTree
          :data="treeData"
          default-node-key="name"
          :default-node-style="handleNodeStyle"
        />
      </div>
    </div>
    <div>
      <h3>自定义节点</h3>
      <div class="canvas-container">
        <VueTree
          :data="treeData"
        >
        <template #node="{data, index}">
          <div class="custom-node">
            <img :src="`/images/${data.name}.svg`" />
            {{ data.name }}
            <span>{{ `#${index+1}` }}</span>
          </div>
        </template>
      </VueTree>
      </div>
    </div>
  </div>
</template>

<style scoped>
.canvas-container {
  width: 700px;
  height: 200px;
  border-radius: 5px;
  border: 1px solid gray;
}

.custom-node {
  width: 80px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  border: 1px solid grey;
  padding: 5px;
}

.custom-node img {
  width: 20px;
  height: 20px;
  margin-right: 5px;
}

.custom-node span {
  margin-left: 5px;
  color: blue;
  font-size: 12px;
}
</style>